<?php
// 更新菜单mig_src字段的脚本

// 数据库连接配置
$host = '127.0.0.1';
$dbname = 'oa5';
$username = 'root';
$password = '123456';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 查询所有需要更新的菜单项
    $sql = "SELECT id, src, title, module FROM oa_admin_rule WHERE src != '' AND menu = 1 AND status = 1";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    
    $menuItems = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo "总共找到 " . count($menuItems) . " 个菜单项需要处理\n";
    
    $updatedCount = 0;
    
    foreach ($menuItems as $item) {
        $newMigSrc = generateMigSrc($item['src'], $item['module']);
        if ($newMigSrc) {
            // 更新数据库
            $updateSql = "UPDATE oa_admin_rule SET mig_src = :mig_src WHERE id = :id";
            $updateStmt = $pdo->prepare($updateSql);
            $updateStmt->execute([
                ':mig_src' => $newMigSrc,
                ':id' => $item['id']
            ]);
            
            echo "更新菜单项 {$item['id']} ({$item['title']}): {$item['src']} => {$newMigSrc}\n";
            $updatedCount++;
        } else {
            echo "警告: 无法为菜单项 {$item['id']} ({$item['title']}) 生成新路径: {$item['src']}\n";
        }
    }
    
    echo "\n成功更新 {$updatedCount} 个菜单项的 mig_src 字段。\n";
    
} catch(PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

/**
 * 根据原始src和module生成新的前端路由路径
 * 
 * @param string $src 原始src路径
 * @param string $module 模块名称
 * @return string|false 新的路由路径或false
 */
function generateMigSrc($src, $module) {
    // 解析原始路径
    $parts = explode('/', $src);
    if (count($parts) < 2) {
        return false;
    }
    
    $moduleDir = $parts[0];  // 模块目录
    $controller = $parts[1]; // 控制器
    $action = $parts[2] ?? 'index'; // 操作方法，默认为index
    
    // 特殊处理一些常见映射
    switch ($moduleDir) {
        case 'home':
            // 系统管理模块映射
            switch ($controller) {
                case 'conf':
                    return '/system/config';
                case 'module':
                    return '/system/modules';
                case 'dataauth':
                    return '/system/module-config';
                case 'rule':
                    return '/system/nodes';
                case 'role':
                    return '/system/roles';
                case 'log':
                    return '/system/logs';
                case 'files':
                    return '/system/files';
                case 'database':
                    if ($action === 'database') {
                        return '/system/database/backup';
                    } else if ($action === 'backuplist') {
                        return '/system/database/restore';
                    }
                    return '/system/database';
                case 'task':
                    return '/system/tasks';
                case 'template':
                    return '/system/templates';
                case 'cate':
                    if ($action === 'enterprise') {
                        return '/system/enterprise';
                    }
                    return '/system/categories';
                case 'area':
                    return '/system/regions';
                default:
                    return '/system/' . $controller;
            }
            
        case 'oa':
            // OA办公模块映射
            switch ($controller) {
                case 'plan':
                    if ($action === 'datalist') {
                        return '/oa/plans';
                    } else if ($action === 'calendar') {
                        return '/oa/plans/calendar';
                    }
                    return '/oa/plans';
                case 'schedule':
                    if ($action === 'datalist') {
                        return '/oa/schedules';
                    } else if ($action === 'calendar') {
                        return '/oa/schedules/calendar';
                    }
                    return '/oa/schedules';
                case 'work':
                    return '/oa/works';
                case 'note':
                    return '/oa/announcements';
                case 'news':
                    return '/oa/news';
                case 'meeting':
                    return '/oa/meetings';
                default:
                    return '/oa/' . $controller;
            }
            
        case 'contract':
            // 合同管理模块映射
            switch ($controller) {
                case 'cate':
                    return '/contract/categories';
                case 'productcate':
                    return '/contract/product-categories';
                case 'product':
                    return '/contract/products';
                case 'services':
                    return '/contract/services';
                case 'supplier':
                    return '/contract/suppliers';
                case 'purchasedcate':
                    return '/contract/purchased-categories';
                case 'purchased':
                    return '/contract/purchased';
                case 'contract':
                    if ($action === 'datalist') {
                        return '/contract/sales';
                    } else if ($action === 'archivelist') {
                        return '/contract/archived';
                    } else if ($action === 'stoplist') {
                        return '/contract/stopped';
                    } else if ($action === 'voidlist') {
                        return '/contract/voided';
                    }
                    return '/contract/sales';
                default:
                    return '/contract/' . $controller;
            }
            
        case 'customer':
            // 客户管理模块映射
            switch ($controller) {
                case 'grade':
                    return '/customer/grades';
                case 'source':
                    return '/customer/sources';
                case 'industry':
                    return '/customer/industries';
                case 'customer':
                    if ($action === 'datalist') {
                        return '/customers';
                    }
                    return '/customers';
                case 'contact':
                    return '/customer/contacts';
                case 'chance':
                    return '/customer/opportunities';
                case 'trace':
                    return '/customer/traces';
                case 'index':
                    if ($action === 'trash') {
                        return '/customers/trash';
                    }
                    return '/customers';
                default:
                    return '/customers/' . $controller;
            }
            
        case 'project':
            // 项目管理模块映射
            switch ($controller) {
                case 'step':
                    return '/projects/steps';
                case 'cate':
                    return '/projects/categories';
                case 'work':
                    return '/projects/works';
                case 'index':
                    if ($action === 'datalist') {
                        return '/projects';
                    }
                    return '/projects';
                case 'task':
                    if ($action === 'datalist') {
                        return '/tasks';
                    } else if ($action === 'hour') {
                        return '/tasks/hours';
                    } else if ($action === 'comment') {
                        return '/tasks/comments';
                    }
                    return '/tasks';
                case 'document':
                    return '/projects/documents';
                default:
                    return '/projects/' . $controller;
            }
            
        case 'finance':
            // 财务管理模块映射
            switch ($controller) {
                case 'expensecate':
                    return '/finance/expense-categories';
                case 'costcate':
                    return '/finance/cost-categories';
                case 'expense':
                    if ($action === 'datalist') {
                        return '/finance/expenses';
                    } else if ($action === 'record') {
                        return '/finance/expense-records';
                    }
                    return '/finance/expenses';
                case 'invoice':
                    if ($action === 'datalist') {
                        return '/finance/invoices';
                    } else if ($action === 'record') {
                        return '/finance/invoice-records';
                    }
                    return '/finance/invoices';
                case 'ticket':
                    if ($action === 'datalist') {
                        return '/finance/tickets';
                    } else if ($action === 'record') {
                        return '/finance/ticket-records';
                    }
                    return '/finance/tickets';
                case 'income':
                    if ($action === 'datalist') {
                        return '/finance/incomes';
                    } else if ($action === 'record') {
                        return '/finance/income-records';
                    }
                    return '/finance/incomes';
                case 'payment':
                    if ($action === 'datalist') {
                        return '/finance/payments';
                    } else if ($action === 'record') {
                        return '/finance/payment-records';
                    }
                    return '/finance/payments';
                default:
                    return '/finance/' . $controller;
            }
            
        case 'user':
            // 人事管理模块映射
            switch ($controller) {
                case 'rewardscate':
                    return '/hr/reward-categories';
                case 'carecate':
                    return '/hr/care-categories';
                case 'basic':
                    return '/hr/basic-data';
                case 'department':
                    return '/hr/departments';
                case 'position':
                    return '/hr/positions';
                case 'user':
                    return '/hr/employees';
                case 'files':
                    return '/hr/archives';
                case 'personal':
                    if ($action === 'change') {
                        return '/hr/transfers';
                    } else if ($action === 'leave') {
                        return '/hr/leaves';
                    }
                    return '/hr/personal';
                case 'rewards':
                    return '/hr/rewards';
                case 'care':
                    return '/hr/cares';
                case 'laborcontract':
                    return '/hr/contracts';
                default:
                    return '/hr/' . $controller;
            }
            
        case 'adm':
            // 行政管理模块映射
            switch ($controller) {
                case 'flow':
                    if ($action === 'modulelist') {
                        return '/admin/flow-modules';
                    } else if ($action === 'catelist') {
                        return '/admin/flow-categories';
                    } else if ($action === 'datalist') {
                        return '/admin/flows';
                    }
                    return '/admin/flows';
                case 'propertycate':
                    return '/admin/asset-categories';
                case 'propertybrand':
                    return '/admin/asset-brands';
                case 'propertyunit':
                    return '/admin/asset-units';
                case 'sealcate':
                    return '/admin/seal-categories';
                case 'basic':
                    return '/admin/basic-data';
                case 'property':
                    if ($action === 'datalist') {
                        return '/admin/assets';
                    } else if ($action === 'repair_list') {
                        return '/admin/asset-repairs';
                    }
                    return '/admin/assets';
                case 'car':
                    if ($action === 'datalist') {
                        return '/admin/cars';
                    } else if ($action === 'repair_list') {
                        return '/admin/car-repairs';
                    } else if ($action === 'protect_list') {
                        return '/admin/car-maintenances';
                    } else if ($action === 'fee_list') {
                        return '/admin/car-fees';
                    }
                    return '/admin/cars';
                case 'meeting':
                    if ($action === 'room') {
                        return '/admin/meeting-rooms';
                    } else if ($action === 'records') {
                        return '/admin/meeting-records';
                    }
                    return '/admin/meetings';
                case 'official':
                    if ($action === 'datalist') {
                        return '/admin/official-docs';
                    } else if ($action === 'pending') {
                        return '/admin/official-docs/pending';
                    } else if ($action === 'reviewed') {
                        return '/admin/official-docs/reviewed';
                    }
                    return '/admin/official-docs';
                case 'seal':
                    if ($action === 'datalist') {
                        return '/admin/seal-applications';
                    } else if ($action === 'record') {
                        return '/admin/seal-records';
                    }
                    return '/admin/seals';
                case 'notecate':
                    return '/admin/note-categories';
                case 'note':
                    return '/admin/notes';
                case 'news':
                    return '/admin/news';
                default:
                    return '/admin/' . $controller;
            }
            
        case 'disk':
            // 网盘模块映射
            switch ($controller) {
                case 'index':
                    if ($action === 'datalist') {
                        return '/disk/personal';
                    } else if ($action === 'sharelist') {
                        return '/disk/shared';
                    } else if ($action === 'clearlist') {
                        return '/disk/trash';
                    }
                    return '/disk';
                default:
                    return '/disk/' . $controller;
            }
            
        case 'lawcase':
            // 案件管理模块映射
            switch ($controller) {
                case 'lawcase':
                    return '/cases';
                case 'party':
                    return '/cases/parties';
                case 'casedoc':
                    return '/cases/documents';
                case 'casecause':
                    return '/cases/causes';
                case 'court':
                    return '/cases/courts';
                case 'progress':
                    return '/cases/progress';
                default:
                    return '/cases/' . $controller;
            }
            
        case 'lawyer':
            // 律师管理模块映射
            switch ($controller) {
                case 'lawfirm':
                    return '/lawyers/firms';
                case 'lawyer':
                    return '/lawyers';
                default:
                    return '/lawyers/' . $controller;
            }
            
        default:
            // 默认处理方式
            return '/' . $moduleDir . '/' . $controller . ($action !== 'index' ? '/' . $action : '');
    }
    
    return false;
}